/**
 * @Author yinyunqi
 * @date 2018年2月7日
 * @Content 后台消息推送
 */
package com.yongqi.controller.back;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.yongqi.model.back.Message;
import com.yongqi.pub.DateFormat;
import com.yongqi.service.back.MessageService;

@Controller
@RequestMapping("/back/msg")
public class BackMsgController {
    @Autowired
    MessageService messageService;
    
    /**
     * 后台推送消息管理
     * @return 推送消息主页
     */
    @RequestMapping("/index")
    public String backMsgIndex() {
	return "back/back_msg";
    }
    /**
     * 后台-推送消息管理-推送消息查询
     */
    @ResponseBody
    @RequestMapping("/query")
    public String backMsgQuery(@RequestParam int pageNumber,int pageSize,String msg_id,String msg_content,String msg_reciver) {
	System.out.println(msg_id+","+msg_content+","+msg_reciver);
	Map<String, String> map = new HashMap<String, String>();
    	map.put("msg_id", msg_id);
    	map.put("msg_content", msg_content);
    	map.put("msg_reciver", msg_reciver);
    	int total = messageService.backMsgQuery(map).size();
    	PageHelper.startPage(pageNumber, pageSize);
    	List<Message> msgList = messageService.backMsgQuery(map);
	JSONObject jsonObject = new JSONObject();
	jsonObject.put("rows", msgList);
	jsonObject.put("total", total);
	return jsonObject.toJSONString();
    }
    
    /**
     * 后台推送消息管理
     * @return 推送消息详细页面
     */
    @RequestMapping("/detail")
    public String backMsgDetail(Model model,@RequestParam String id) {
	Map<String, String> map = new HashMap<String, String>();
    	map.put("id", id);
	List<Message> msg = messageService.backMsgQueryById(map);
	model.addAttribute("msg", msg);
	return "back/back_msg_detail";
    }
    
    /**
     * 后台推送消息管理
     * @return 推送消息修改页面
     */
    @RequestMapping("/edit")
    public String backMsgEdit(Model model,@RequestParam String id) {
	Map<String, String> map = new HashMap<String, String>();
    	map.put("id", id);
	List<Message> msg = messageService.backMsgQueryById(map);
	model.addAttribute("msg", msg);
	return "back/back_msg_edit";
    }
    /**
     * 后台推送消息管理
     * @return 推送消息删除
     */
    @ResponseBody
    @RequestMapping("/delete")
    public String backMsgDelete(@RequestParam String id) {
	Map<String, String> map = new HashMap<String, String>();
    	map.put("id", id);
	messageService.backMsgDetele(map);
	JSONObject jsonObject = new JSONObject();
	jsonObject.put("result", "success");
	return jsonObject.toJSONString();
    }
    /**
     * 后台推送消息管理
     * @return 消息推送页面
     */
    @RequestMapping("/push")
    public String backMsgPush() {
	return "back/back_msg_push";
    }
    /**
     * 后台推送消息管理
     * @return 消息推送发送
     */
    @ResponseBody
    @RequestMapping("/push/send")
    public String backMsgPushSend(@RequestParam String content,String author,String reciver,String type) {
	System.out.println(content+","+author+","+reciver+","+type);
	Message message = new Message();
	message.setCreated_time(DateFormat.dateFormate());
	message.setContent(content);
	message.setAuthor(author);
	message.setReciver(reciver);
	message.setType(type);
	messageService.backMsgPushSend(message);
	JSONObject jsonObject = new JSONObject();
	jsonObject.put("result", "success");
	return jsonObject.toJSONString();
    }
}

